package debux.apps.mltr.app;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import debux.apps.mltr.model.AlbumInfo;
import debux.apps.mltr.model.DatabaseController;

public class AlbumActivity extends ActivityGroup  {
	AlbumListAdapter adapter;
	public static ArrayList<AlbumInfo> albumList;
	ListView albumListView;

	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.album_tab_frame);
		albumList = new ArrayList<AlbumInfo>();
		DatabaseController.getAllAlbums(albumList);
		albumListView = (ListView)findViewById(R.id.albumListView);
		adapter = new AlbumListAdapter(this, R.layout.album_list_item, albumList);
		albumListView.setAdapter(adapter);
		albumListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AlbumActivity.this,AlbumDetailsAcitivity.class);
				AlbumDetailsAcitivity.mAlbumId = arg2;
		        //replaceContentView("tab_albums_details", intent,Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//super.onBackPressed();
//		Log.i("DatTX","AlbumActivity: onBackPressed");
//		Intent intent = getIntent();
//		this.replaceContentView("tab_albums", intent,
//				Intent.FLAG_ACTIVITY_CLEAR_TOP);
		super.onBackPressed();
	}
	public void replaceContentView(String id, Intent newIntent, int flag) {
	    View view =getLocalActivityManager()
	            .startActivity(id,
	                    newIntent.addFlags(flag))
	            .getDecorView();
	    this.setContentView(view);

	}
	
	class AlbumListAdapter extends ArrayAdapter<AlbumInfo>{
		ArrayList<AlbumInfo> albums;
		
		public AlbumListAdapter(Context context, int textViewResourceId,
				ArrayList<AlbumInfo> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
			albums = objects;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.album_list_item, null);
            }
            //ImageView albumThumb = (ImageView)v.findViewById(R.id.album_thumb);
            TextView albumName = (TextView)v.findViewById(R.id.album_name);
            TextView albumReleased = (TextView)v.findViewById(R.id.album_released);
            albumName.setText(albums.get(position).getName());
            albumReleased.setText(albums.get(position).getReleased());
            return v;
		}
	}
}
